package lpg;

class Spell
{
    static int index(String str1, String str2)
    {
        int len1 = str1.length();
        int len2 = str2.length();

        if (len1 == 1 && len2 == 1)
        {
            //
            //  Singleton mispellings:
            //
            //  ;      <---->     ,
            //
            //  ;      <---->     :
            //
            //  .      <---->     ,
            //
            //  '      <---->     "
            //
            char c1 = str1.charAt(0);
            char c2 = str2.charAt(0);
            if ((c1 == ';'  &&  c2 == ',')  ||
                (c1 == ','  &&  c2 == ';')  ||
                (c1 == ';'  &&  c2 == ':')  ||
                (c1 == ':'  &&  c2 == ';')  ||
                (c1 == '.'  &&  c2 == ',')  ||
                (c1 == ','  &&  c2 == '.')  ||
                (c1 == '\'' &&  c2 == '\"')  ||
                (c1 == '\"' &&  c2 == '\''))
                    return 3;
        }

        //
        // Scan the two strings. Increment "match" count for each match.
        // When a transposition is encountered, increase "match" count
        // by two but count it as an error. When a typo is found, skip
        // it and count it as an error. Otherwise we have a mismatch; if
        // one of the strings is longer, increment its index, otherwise,
        // increment both indices and continue.
        //
        // This algorithm is an adaptation of a boolean misspelling
        // algorithm proposed by Juergen Uhl.
        //
        int count = 0;
        int prefixLength = 0;
        int numErrors = 0;
        int i1 = 0;
        int i2 = 0;

        str1 = str1.toLowerCase();
        str2 = str1.toLowerCase();

        while (i1 < len1 && i2 < len2)
        {
            if (str1.charAt(i1) == str2.charAt(i2))
            {
                count++;
                i1++;
                i2++;
                if (numErrors == 0)
                    prefixLength++;
            }
            else if (i1 < len1 - 1 && i2 < len2 - 1)
            {
                if (str1.charAt(i1 + 1) == str2.charAt(i2) && str1.charAt(i1) == str2.charAt(i2 + 1))
                {
                    count += 2;
                    i1 += 2;
                    i2 += 2;
                    numErrors++;
                }
                else if (str1.charAt(i1 + 1) == str2.charAt(i2 + 1))
                {
                    i1++;
                    i2++;
                    numErrors++;
                }
            }
            else
            {
                if ((len1 - i1) > (len2 - i2))
                    i1++;
                else if ((len2 - i2) > (len1 - i1))
                    i2++;
                else
                {
                    i1++;
                    i2++;
                }
                numErrors++;
            }
        }

        if (i1 < len1 || i2 < len2)
            numErrors++;

        if (numErrors > (Math.min(len1, len2) / 6 + 1))
             count = prefixLength;

        return (count * 10 / (len1 + numErrors));
    }

    private Spell()
    {
    }

}
